package com.yzh.croom.service;

import com.yzh.croom.model.domain.Role;
import com.yzh.croom.model.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //获取用户信息
        User user = userService.getUser(s);
        //获取权限数据
        List<Role> authorities = userService.getUserAuthorities(s);
        //对用户权限进行封装
        List<SimpleGrantedAuthority> rollcollect = authorities.stream().map(authority -> new SimpleGrantedAuthority(authority.getRole())).collect(Collectors.toList());
        //定义用户详情类
        if (user != null) {
            UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getAccount(), user.getPassword(), rollcollect);
            return userDetails;
        } else {
            throw new UsernameNotFoundException("该账号不存在");
        }
    }
}
